﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/7/21 14:55:23
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary.Forms;
using System;
using System.Threading;

namespace CommonLibrary
{
    /// <summary>
    /// 名称与图标
    /// </summary>
    public class SofrwareNameLogo
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string SoftwareName { get; set; } = null;
        /// <summary>
        /// 图标
        /// </summary>
        public System.Drawing.Image Logo { get; set; } = null;
    }
    /// <summary>
    /// 启动画面操作类
    /// </summary>
    public class SplashHandler
    {
        private static HWFrmSplash SPLASH_FORM = null;
        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sofrwareNameLogo"></param>
        public static void Start(SofrwareNameLogo sofrwareNameLogo = null)
        {
            new Thread(OpenSplashForm).Start(sofrwareNameLogo);
        }
        private static void OpenSplashForm(object sofrwareNameLogo)
        {
            SPLASH_FORM = new HWFrmSplash();
            SPLASH_FORM.Opacity = 1;
            SPLASH_FORM.BringToFront();
            if (sofrwareNameLogo != null && sofrwareNameLogo is SofrwareNameLogo)
            {
                var s = sofrwareNameLogo as SofrwareNameLogo;
                SPLASH_FORM.SetSystemName($"{s.SoftwareName}");
                SPLASH_FORM.SetLogo(s.Logo);
            }
            System.Windows.Forms.Application.Run(SPLASH_FORM);
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public static void Close()
        {
            if (SPLASH_FORM != null)
            {
                SPLASH_FORM.Invoke(new EventHandler(SPLASH_FORM.Kill));
                SPLASH_FORM.Dispose();
                SPLASH_FORM = null;
            }
        }
        /// <summary>
        /// 变更显示信息
        /// </summary>
        /// <param name="text"></param>
        public static void ChangeInfo(string text)
        {
            if (SPLASH_FORM != null && !SPLASH_FORM.IsHandleCreated)
            {
                Thread.Sleep(100);
            }
            if (SPLASH_FORM != null)
            {
                SPLASH_FORM.Invoke(new EventHandler(SPLASH_FORM.ChangeInfo), text);
            }
        }
        /// <summary>
        /// 前端显示
        /// </summary>
        public static void BringTop()
        {
            if (SPLASH_FORM != null)
            {
                SPLASH_FORM.Invoke(new EventHandler(SPLASH_FORM.BringTop));
            }
        }
    }
}
